package com.qdlc.p2p.biz.task.impl;

import org.apache.log4j.Logger;

import com.qdlc.p2p.biz.service.FinanFlowService;
import com.qdlc.p2p.biz.task.flow.FlowAbstractLoanTask;
import com.qdlc.p2p.biz.task.flow.FlowJobQueue;
import com.qdlc.p2p.biz.task.flow.FlowValueEvent;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.util.BeanUtil;

/**
 * 流转标投标业务线程
 * @Title: flowTenderTask.java
 * @Package com.rd.p2p.core.quartz.task
 * @Description: 投标业务处理类
 * @author Moon.Liu
 * @date 2014年12月2日 上午10:56:03
 * @version V2.0
 */
public class FlowTenderTask extends FlowAbstractLoanTask {
	private Logger logger = Logger.getLogger(FlowTenderTask.class);

	public FlowTenderTask() {
		super();
		task.setName("Tender.Task");
	}

	@SuppressWarnings("unchecked")
	@Override
	public void doLoan() {

		FlowJobQueue<FlowValueEvent> queue = FlowJobQueue.getTenderInstance();
      
		FinanFlowService finanFlowService = (FinanFlowService) BeanUtil.getBean("finanFlowService");
		while (!queue.isEmpty()) {
			FlowValueEvent event = queue.peek();
			if (event != null) {
				String result = "success";
				try {
					if ("flowtender".equals(event.getOperate())) {// 正常投标
						finanFlowService.addFlowInvestor(event.getFlowProject(), event.getProjectModel());
					} 
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					if (e instanceof BusinessException) {// 业务异常，保存业务处理信息
						result = e.getMessage();
					} else {
						result = "系统异常，业务处理失败";
					}
				} finally {
				    queue.remove(event);
				}
				if (event.getResultFlag() != null) {// 在需要保存系统处理信息的地方直接保存进来
					Global.TASK_RESULT_MAP.put(event.getResultFlag(), result);
				}
			}
		}

	}

	@Override
	public Object getLock() {
		return FlowTenderTask.TENDER_STATUS;
	}

}
